package com.zp.qualitypractice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zp.qualitypractice.Adapter.SwipeViewBaseAdapter;
import com.zp.qualitypractice.Adapter.SwipeViewBaseViewHolder;
import com.zp.qualitypractice.activity.ScrollActivity;
import com.zp.qualitypractice.list.chain.ChainRecyclerDemoActivity;
import com.zp.qualitypractice.list.demo.RefreshSwipeLayoutActivity;
import com.zp.qualitypractice.list.RefreshListView;
import com.zp.qualitypractice.SwipeLayout.SwipeManager;
import com.zp.qualitypractice.SwipeLayout.SwipeView;
import com.zp.qualitypractice.bar.BarDemoActivity;
import com.zp.qualitypractice.rx.RxDemoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.mainListView)
    ListView mainListView;
    @BindView(R.id.refreshList)
    RefreshListView refreshList;
    List<String> mData ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        initSwipeViewPart();

    }
    private void initSwipeViewPart(){
//        Adapter adapter = new Adapter(this,creteData(),R.layout.item_main_swipe);
//        mainListView.setAdapter(adapter);
        DemoAdapter demoAdapter = new DemoAdapter(this,creteData());
        refreshList.setAdapter(demoAdapter);

    }
    private List creteData() {
        mData = new ArrayList<>();
        for (int i=0;i<20;i++) {
            mData.add("测试数据 "+i);
        }
        return mData;
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
            Intent intent = new Intent(this, RefreshSwipeLayoutActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_gallery) {
            startActivity(new Intent(this,DemoActivity.class));
        } else if (id == R.id.nav_slideshow) {
            startActivity(new Intent(this,RxDemoActivity.class));
        } else if (id == R.id.nav_manage) {
            startActivity(new Intent(this,BarDemoActivity.class));
        } else if (id == R.id.nav_share) {
            startActivity(new Intent(this,ChainRecyclerDemoActivity.class));
        } else if (id == R.id.nav_send) {
            startActivity(new Intent(this,ScrollActivity.class));
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    class Adapter extends SwipeViewBaseAdapter<String>{

        public Adapter(Context mContext, List<String> mdata, int mLayoutId) {
            super(mContext, mdata, mLayoutId);
        }

        @Override
        public void convert(SwipeViewBaseViewHolder holder, final String s) {
            holder.setText(R.id.tv_name, s);
            SwipeView swipeLayout = holder.getViewByID(R.id.item_swipe_view);
            swipeLayout.setOnItemClickListener(new SwipeView.OnItemClickListener() {
                @Override
                public void onItemClick() {
                    Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                }
            });

            swipeLayout.getDeleteView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SwipeManager.getInstance().quickClose();
                    mData.remove(s);
                    notifyDataSetChanged();
                    Toast.makeText(mContext, "数据总数: " + mData.size(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    class DemoAdapter extends BaseAdapter{
        private Context context;
        private List<String> data;

        public DemoAdapter(Context context, List<String> data) {
            this.context = context;
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view =View.inflate(context,R.layout.item_text,null);
            TextView textView = (TextView) view.findViewById(R.id.text);
            textView.setText(mData.get(i)+"");
            return view ;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        ALog.v("后台监听"+App.getApp().getAppCount());
    }

    @Override
    protected void onStop() {
        super.onStop();
        ALog.v("后台监听"+App.getApp().getAppCount());
    }
}
